package com.zmx.job.admin.controller;

import com.zmx.core.dto.HeartInfo;
import com.zmx.core.server.ClientInstanceInfo;
import com.zmx.job.admin.dto.NodeInfoDto;
import com.zmx.job.admin.entity.MallJobClient;
import com.zmx.job.admin.repository.MallJobClientRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 注册节点控制器
 * @ClassName: NodeInfoController
 * @Author zhaomxr
 * @Date 2021-12-16 16:23
 */
@RestController
@RequestMapping("node")
public class NodeInfoController {

    @Autowired
    private MallJobClientRepository mallJobClientRepository;

    @GetMapping("getNodeInfoInstance")
    public List<NodeInfoDto> getNodeInfoInstance(@RequestParam(required = false) String node) {
        Map<String, HeartInfo> infoInstance = new HashMap<String, HeartInfo>(16);
        if (StringUtils.isEmpty(node)) {
            infoInstance = ClientInstanceInfo.getInfoInstance();
        } else {
            HeartInfo heartInfo = ClientInstanceInfo.getInfoInstance(node);
            infoInstance.put(node, heartInfo);
        }


        List<NodeInfoDto> nodeInfoDtos = null;

        if (infoInstance != null && infoInstance.size() > 0) {

            Map<String, List<MallJobClient>> clientMap = new HashMap<String, List<MallJobClient>>(16);
            List<MallJobClient> mallJobClients = mallJobClientRepository.findByOnLine(1);

            if (!CollectionUtils.isEmpty(mallJobClients)) {
                clientMap = mallJobClients.stream().collect(Collectors.groupingBy(mallJobClient -> mallJobClient.getPort() + ":" + mallJobClient.getPort()));
            }

            nodeInfoDtos = new ArrayList<NodeInfoDto>(infoInstance.size());
            //组装节点信息
            for (String name : infoInstance.keySet()) {
                HeartInfo hertInfo = infoInstance.get(name);

                NodeInfoDto nodeInfoDto = new NodeInfoDto();
                nodeInfoDto.setNode(name);
                nodeInfoDto.setLasttime(hertInfo.getLasttime());
                //权重，默认为 1
                int weight = 1;
                if (clientMap.get(name) != null && clientMap.get(name).size() > 0) {
                    weight = clientMap.get(name).get(0).getWeight();
                }
                nodeInfoDto.setWeight(weight);
                boolean online = ClientInstanceInfo.getOnLineInstance(name);
                nodeInfoDto.setOnline(online);

                nodeInfoDtos.add(nodeInfoDto);
            }

        }

        return nodeInfoDtos;
    }

    @GetMapping("getNodeOnLineInstance")
    public Object getNodeOnLineInstance(@RequestParam(required = false) String node) {
        if (StringUtils.isEmpty(node)) {
            return ClientInstanceInfo.getOnLineInstance();
        } else {
            return ClientInstanceInfo.getOnLineInstance(node);
        }
    }
}
